package com.EAsystem.controller.admin;

import com.EAsystem.annotation.RequireRole;
import com.EAsystem.entity.College;
import com.EAsystem.service.CollegeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/admin")
@RequireRole({1}) // 只有管理员可以访问
public class CollegeController {

    @Autowired
    private CollegeService collegeService;

    @GetMapping("/college")
    public String listCollege(@RequestParam(defaultValue = "1") int page,
                              @RequestParam(defaultValue = "5") int size,
                              Model model,
                              @RequestParam(required = false) String collegeName) {

        PageInfo<College> pageInfo;
        if (collegeName != null && !collegeName.isEmpty()) {
            College college = new College();
            college.setName(collegeName);
            pageInfo = collegeService.findByNameWithPage(college, page, size);
        } else {
            pageInfo = collegeService.findAllByPage(page, size);
        }
        if (pageInfo == null) pageInfo = new PageInfo<>();
        model.addAttribute("colleges", pageInfo.getList());
        model.addAttribute("currentPage", page);
        model.addAttribute("pageSize", size);
        model.addAttribute("totalPages", pageInfo.getPages());
        model.addAttribute("totalRecords", pageInfo.getTotal());
        model.addAttribute("collegeName", collegeName);
        return "admin/college";
    }

    @PostMapping("/college/add")
    public String addCollege(College college) {
        String now = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
        college.setCreateTime(now);
        college.setUpdateTime(now);
        collegeService.save(college);
        return "redirect:/admin/college";
    }

    @PostMapping("/college/edit")
    public String editCollege(College college) {
        String now = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
        college.setUpdateTime(now);
        collegeService.edit(college);
        return "redirect:/admin/college";
    }

    @GetMapping("/college/delete/{id}")
    public String deleteCollege(@PathVariable Integer id) {
        collegeService.delete(id);
        return "redirect:/admin/college";
    }

    @GetMapping("/college/view/{id}")
    @ResponseBody
    public College viewCollege(@PathVariable Integer id) {
        try {
            return collegeService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @GetMapping("/college/batchDelete")
    public String batchDelete(@RequestParam("ids") String ids) {
        java.util.List<Integer> idList = new java.util.ArrayList<>();
        for (String id : ids.split(",")) {
            idList.add(Integer.valueOf(id));
        }
        collegeService.batchDelete(idList);
        return "redirect:/admin/college";
    }
}